package com.monicash.monicash.netconfig

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

sealed class MonicashResponse<out T> {
    data class Success<out T>(val data: T? = null) : MonicashResponse<T>()
    data class Error(val code: Int?, val message: String?) : MonicashResponse<Nothing>()
}

@OptIn(ExperimentalContracts::class)
inline fun <reified T> MonicashResponse<T>.isSuccess(): Boolean {
    contract {
        returns(true) implies (this@isSuccess is MonicashResponse.Success<T>)
        returns(false) implies (this@isSuccess is MonicashResponse.Error)
    }
    return this is MonicashResponse.Success<T>
}

inline fun <reified T> MonicashResponse<T>.data(): T? {
    return if (this is MonicashResponse.Success) {
        this.data
    } else {
        null
    }
}